package com.lwz.order.service.impl;

import com.lwz.order.client.EduClient;
import com.lwz.order.client.UcenterClient;
import com.lwz.order.entity.Order;
import com.lwz.order.mapper.OrderMapper;
import com.lwz.order.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lwz.order.util.OrderNoUtil;
import com.lwz.util.orderVo.CourseWebVoOrder;
import com.lwz.util.orderVo.UcenterMemberOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author Lw中
 * @since 2020-11-20
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    /**
     * LWZ TODO : 2020/11/20 订单生成接口
     */

    @Autowired
    private EduClient eduClient;

    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public String addCourseOrder(String courseId, String userId) {
        // 先通过课程id获取课程的相关信息
        CourseWebVoOrder orderCourseInfo = eduClient.getOrderCourseInfo(courseId);
        // 通过用户的id获取用户信息
        UcenterMemberOrder userInfo = ucenterClient.getUserInfo(userId);
        // 通过上面的信息创建订单
        //创建Order对象，向order对象里面设置需要数据
        Order order = new Order();
        //通过工具类生成订单号
        order.setOrderNo(OrderNoUtil.getOrderNo());
        //课程id
        order.setCourseId(courseId);
        order.setCourseTitle(orderCourseInfo.getTitle());
        order.setCourseCover(orderCourseInfo.getCover());
        order.setTeacherName(orderCourseInfo.getTeacherName());
        order.setTotalFee(orderCourseInfo.getPrice());
        order.setMemberId(userId);
        order.setMobile(userInfo.getMobile());
        order.setNickname(userInfo.getNickname());
        //订单状态（0：未支付 1：已支付）
        order.setStatus(0);
        //支付类型 ，微信1
        order.setPayType(1);
        baseMapper.insert(order);
        return order.getOrderNo();
    }
}
